package org.jypj.dev.controller.back;

import org.jypj.dev.constant.BaseConstant;
import org.jypj.dev.controller.BaseController;
import org.jypj.dev.dao.UserDao;
import org.jypj.dev.model.User;
import org.jypj.dev.util.PropertyUtil;
import org.jypj.dev.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ChenYu on 2017-06-05.
 */
@Controller
@RequestMapping("back")
public class LoginController extends BaseController {

    @Resource
    private UserDao userDao;


    /**
     * 跳转到登录页
     *
     * @return
     */
    @RequestMapping(value = "login")
    public String toLogin() {

        return "back/login";
    }

    /**
     * 跳转到后台框架页
     *
     * @return
     */
    @RequestMapping(value = "main")
    public String toMain() {
        return "back/main";
    }

    /**
     * 登录
     *
     * @param userName
     * @param password
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "check")
    public Result login(String userName, String password, HttpSession session) {
        Result result = new Result();
        try {
            User user = userDao.selectObjectByUserName(userName);
            if (user == null || !password.equals(user.getPassword())) {
                result.setCode(-2);
                return result;
            }
            session.setAttribute(BaseConstant.BACK_SESSION_USER, user);
        } catch (Exception e) {
            result.setErrorStatus(e);
        }
        return result;
    }

    /**
     * 注销登录
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:login";
    }
}
